﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConjureBivouac2012.Object;
using Microsoft.Xna.Framework;

namespace ConjureBivouac2012.Physic
{
    public static class Physic
    {

        // Vecteur de detection
        // Direction du vaisseau
        Vector2 tangent;

        // Velocite relative d'un projectile
        Vector2 relvel;

        // Collision entre deux entitee
        // Verifie aussi les collision avec les balles de cet ennemie
        public bool collision(Entity ship, Entity otherShip)
        {
            /**
            // Distance of collision
            float collideLenght = ship.sprite.Bounds.

            // Position respective des vaisseaux
            tangent = ship.sprite.Position;
            relvel = new Vector2(0 - Direction.X, 0 - Direction.Y);

            tangent.X = -(tangent.X - relvel.X);
            tangent.Y = -(tangent.Y - relvel.Y);
            tangent.Normalize();
            relvel = new Vector2(0 - Direction.X, 0 - Direction.Y);


            lenght = Vector2.Dot(relvel, tangent);

            tangent.Y = -(sprite.Position.X - player.sprite.Position.X);
            tangent.X = sprite.Position.Y - player.sprite.Position.Y;
            tangent.Normalize();
            //Vector2 relvel = new Vector2(0 - Direction.X, 0 - Direction.Y);
            float lenght = Vector2.Dot(relvel, tangent);
            Vector2 componentTangent = new Vector2();
            Vector2.Multiply(ref tangent, lenght, out componentTangent);
            Vector2 perpendiculaire = relvel - componentTangent;

            Direction.X += 2 * perpendiculaire.X;
            Direction.Y += 2 * perpendiculaire.Y; */

            return (ship.sprite.Bounds.Intersects(otherShip.sprite.Bounds));
        }

        public bool collision (Entity ship, List<Entity> bullets)
        {
            bool collided = false;

            foreach(var bullet in bullets)
            {
                if (ship.sprite.Bounds.Intersects(bullet.sprite.Bounds)) collided = true;
            }
            return collided;
        }

        // Check if a ship has been hit
        public bool collisionBullet(Entity ship, Vector2 shield, int sAngle, int angle, List<Entity> bullets)
        {
            bool collided = false;

            foreach (var bullet in bullets)
            {
                if (ship.sprite.Bounds.Intersects(bullet.sprite.Bounds))
                {
                    shield.Normalize();
                    float enemyAngle = -(float)Math.Atan2(shield.Y, shield.X);
                    enemyAngle = (float)(enemyAngle * 360 / (2 * Math.PI));
                    if (enemyAngle < 0)
                        enemyAngle += 360;
                    else if (enemyAngle >= 360)
                        enemyAngle -= 360;

                    if (sAngle - angle > 360)
                    {
                        if (enemyAngle >= sAngle || enemyAngle <= (sAngle - angle) % 360) ;
                        else
                        {
                            collided = true;
                        }
                    }
                    else
                    {
                        if (enemyAngle < sAngle && enemyAngle > sAngle + angle) collided = true;

                    }
                }

                return collided;
            }
        }

        public void rebound(Entity ship, Entity otherShip)
        {
        }

        public void rebound(Entity ship, List<Entity> bullets)
        {
        }

        public void rebound(List<Entity> bullets)
        {
        }

        // Compute the radius of a circel
        private float getRadius(int width, int height)
        {

        }

    }
}
